<?php
// +----------------------------------------------------------------------
// | 管理
// +----------------------------------------------------------------------
// | Author: 李乾坤 <261001642@qq.com>
// +----------------------------------------------------------------------
class QiniuAction extends CommonAction {
	public	$accessKey = 'FwbFcOniG6Af7osdFaoXepZWY25SxH6fmM1dwXre';
	public	$secretKey = 'Ym-jNPMMYLLCf13IxuuCC_LkeL9gTDC0pyqbVGyf';
	function index(){
		Vendor("qiniu.rs");
		Vendor("qiniu.io");
		$bucket = 'sssui';
		$accessKey = 'FwbFcOniG6Af7osdFaoXepZWY25SxH6fmM1dwXre';
		$secretKey = 'Ym-jNPMMYLLCf13IxuuCC_LkeL9gTDC0pyqbVGyf';
		Qiniu_SetKeys($accessKey, $secretKey);
		
		$putPolicy = new Qiniu_RS_PutPolicy($bucket);
		$upToken = $putPolicy->Token(null);
		$this->assign('upToken',$upToken);

		$Attach=M('Attach');
		$this->assign('list',$Attach->where('module="Qiniu"')->order('id desc')->select());
		$this->display();
	}
	function saveAttach(){
		$_POST['module']="Qiniu";
		$_POST['savepath']="http://sssui.u.qiniudn.com/";
		$_POST['create_time']=time();

		$Attach=D('Attach');
        if(false === $Attach->create()) {
            $this->error($Attach->getError());
        }
        //保存当前数据对象
        if($result = $Attach->add()) { //保存成功
            $this->success(L('新增成功'));
        }else {
            //失败提示
            $this->error(L('新增失败'));
        }
	}
	function delete(){
		$id=$_GET['id'];
		$Attach=M('Attach');
		$vo=$Attach->find($id);
		Vendor("qiniu.rs");
		$bucket = 'sssui';
		$filename = $vo['savename'];
		Qiniu_SetKeys($this->accessKey, $this->secretKey);
		$client = new Qiniu_MacHttpClient(null);
		$err = Qiniu_RS_Delete($client, $bucket, $filename);
		if ($err !== null) {
		    var_dump($err);
		} else {
			$Attach->delete();
		    $this->success('删除成功!');
		    echo "Success!";
		}
	}
	function upload(){
		Vendor("qiniu.rs");
		Vendor("qiniu.io");
		$bucket = 'sssui';
		$accessKey = 'FwbFcOniG6Af7osdFaoXepZWY25SxH6fmM1dwXre';
		$secretKey = 'Ym-jNPMMYLLCf13IxuuCC_LkeL9gTDC0pyqbVGyf';
		Qiniu_SetKeys($accessKey, $secretKey);
		
		$putPolicy = new Qiniu_RS_PutPolicy($bucket);
		$upToken = $putPolicy->Token(null);
		
		//直接写入文件内容
		
		/*$filename = "file_name1";
		$filecontent="Qiniu Storage!";
		list($ret, $err) = Qiniu_Put($upToken, $filename, $filecontent, null);
		echo "====> Qiniu_Put result: \n";
		if ($err !== null) {
		    var_dump($err);
		} else {
		    var_dump($ret);
		}*/
		
		$putExtra = new Qiniu_PutExtra();
		$putExtra->Crc32 = 1;
		list($ret, $err) = Qiniu_PutFile($upToken, $key1, __file__, $putExtra);
		echo "====> Qiniu_PutFile result: \n";
		if ($err !== null) {
		    var_dump($err);
		}else {
		    var_dump($ret);
		}
	}

}
?>